﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;
using ZLDC.Topo.BLL.Estate;

namespace ZLDC.Estate.PM
{
    public partial class HousingChangeReason : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // typeid 1当前盘转到资料盘 2当前盘转到无效盘
        protected void imgBtnOK_Click(object sender, EventArgs e)
        {
            string outmsg = "";

            string remark = "";
            remark += "[转盘原因:" + this.rblReason.SelectedItem.Text;
            if (this.rblCompany.SelectedIndex != -1)
                remark += ",成交公司:" + this.rblCompany.SelectedItem.Text;
            remark += "]";

            int typeid = _Request.GetInt("typeid", 0);
            int id = _Request.GetInt("id", 0);

            if (id <= 0)
            {
                _JavaScript.Alert(this.Page, "请先选择您要操作的盘源");
                return;
            }

            HousingInfo info = HousingBLL.p_housing_detail(id);
            if(info == null)
                return;

            // 1分行经理及以上级别
            string SalesManagerStation = _Utility.GetWebConfigSetting("SalesManagerStation"); 

            //判断是否是本部盘源并且是本部门管理人员
            string deptlist = CacheUtility.Cache_GetAllChildDept(this.info.m_sDeptID);

            if ((deptlist.Contains(info.MarkUDeptId.ToString()) && SalesManagerStation.Contains(_Utility.GetInfoBySession().m_sStation.ToString())) || this.info.m_sRoleID == "1" || _Utility.GetInfoBySession().m_sUserID == info.MarkUId.ToString())
            {
            }
            else
            {
                Response.Write("<script type=\"text/javascript\">alert('您无权将该盘转为资料盘'); window.close();</script>");
                return;
            }

            if (typeid == 1)
            {
                outmsg = ZLDC.Topo.BLL.Estate.HousingBLL.HousingStatus(id, 2, remark);  // 经理以上级别直接转，添加跟进  
                //当前转资料清空钥匙，独家，反签                
                ZLDC.Topo.BLL.Estate.HousingBLL.UpdateKeys(id, 0, "", "", "", 0, "", null, 0, "", "");

                string strinfo = "当前转资料：清空钥匙，独家，反签信息{";
                if (info.KeepKey != "" && info.KeyNo != "") 
                {
                    strinfo += "钥匙人：" + info.KeepKey + ",钥匙编号：" + info.KeyNo+",";
                }
                else if (info.OnlyUser != "") 
                {
                    strinfo += "独家人员：" + info.OnlyUser + ",";
                }
                else if (info.CtyContUser != "") 
                {
                    strinfo+="反签人员："+info.CtyContUser+",";
                }
                strinfo += "}";
                if (strinfo.Contains("：")) 
                {
                    HousingFollowBLL.p_housingFollow_save(0, id, "", _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID.ToString()), _Utility.GetInfoBySession().m_sDeptName, _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID.ToString()), _Utility.GetInfoBySession().m_sUserName, strinfo, 0, "");
                }               
            }
            else
            {
                outmsg = ZLDC.Topo.BLL.Estate.HousingBLL.HousingStatus(id, 3, remark);
            }

            ///当前转资料(无效)取消推荐..
            //ZLHome.CallServices.ZhHomeWcf_HouseInfos.HouseInfosClient House = new ZLHome.CallServices.ZhHomeWcf_HouseInfos.HouseInfosClient();

            //IList<Int32> Id1 = new List<Int32>();
            //Id1.Add(id);
            //String json = House.Remove(1, "18c3365c-ff97-455c-9f4c-b802d31c5363", Id1.ToArray());


            ////当前转资料(无效)删除推广信息
            //GeneralZlhomeResult grs = GeneralZlhomeResult.GetTransformData(json);

            //string status = grs.result.Status;
            //if (status == "ok")
            //{
            //    ZLDC.Topo.BLL.Estate.ZlhomeHousingExpandBLL.HousingStatusChanges(id);
            //} 
     

            // 更新缓存
            if (id > 0)
            {
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_JSON + id.ToString());
                _Cache.Remove(CacheConst.CACHE_HOUSING_DETAIL_INFO + id.ToString());
                _Cache.RemoveLike(CacheConst.CACHE_HOUSING_FOLLOW_JSON + id);
            }

            Response.Write("<script type=\"text/javascript\">window.close();</script>");
        }
    }

}
